import CellType from '../celltypes';
import utils from '../../../utils';
import { Tools } from 'weaver-mobile';
const { getLabel } = Tools;

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const { sheet, table, col: pos, colCount: addCols = 1 } = options;
        try {
            sheet.suspendPaint();
            const { row: dataRow } = table.dataRange();
            if (isUndo) {
                const { name, row, col, rowCount, colCount, columns, cellTypes, bindingPath, labels, showHeader } = options.old;
                sheet.tables.remove(name);
                const oldTable = sheet.tables.add(name, row, col, rowCount, colCount, undefined, { showHeader });
                oldTable.filterButtonVisible(false);
                oldTable.bindColumns(columns.map(column => new window.GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField)));
                oldTable.bindingPath(bindingPath);
                cellTypes.forEach((cellType, index) => {
                    sheet.getCell(dataRow, col + index).cellType(cellType);
                });
                labels.forEach((label, index) => {
                    const tempCol = col + colCount + index;
                    sheet.setValue(row, tempCol, label);
                });
            } else {
                const FieldCellType = CellType('FieldCellType');
                const tableJSON = table.toJSON();
                const { name, row, col, rowCount, colCount, columns, bindingPath, showHeader } = tableJSON;
                const old = options.old = {};
                old.name = name;
                old.row = row;
                old.col = col;
                old.rowCount = rowCount;
                old.colCount = colCount;
                old.columns = columns;
                old.bindingPath = bindingPath;
                old.showHeader = showHeader;
                old.cellTypes = columns.map((d, index) => {
                    return sheet.getCell(dataRow, col + index).cellType();
                });
                old.labels = [];
                const fieldNames = old.cellTypes.filter(d => !!d).map(d => d.fieldName.toLowerCase());
                for (let colIndex = 0; colIndex < addCols; colIndex++) {
                    const tempCol = col + colIndex + colCount;
                    old.cellTypes.push(sheet.getCell(dataRow, tempCol).cellType());
                    old.labels.push(sheet.getValue(row, tempCol));
                }
                sheet.tables.remove(name);
                const newColCount = colCount + addCols;
                const newTable = sheet.tables.add(name, row, col, rowCount, newColCount, undefined, { showHeader });
                newTable.filterButtonVisible(false);
                const newColumns = [];
                let currentColIndex = col + newColCount - 1;
                let size = columns.length;
                for (let index = size - 1; index >= 0; index--) {
                    const column = columns[index];
                    if (index + col === pos) {
                        let fieldIndex = 0;
                        for (let colIndex = 0; colIndex < addCols; colIndex++) {
                            let newField = `zd${++fieldIndex}`;
                            while (fieldNames.indexOf(newField) > -1) {
                                newField = `zd${++fieldIndex}`;
                            }
                            fieldNames.push(newField);
                            const label = `${getLabel('502711', '字段') + fieldIndex}`;
                            newColumns.push(new window.GC.Spread.Sheets.Tables.TableColumn(newField).name(label).dataField(newField));
                            const newCellType = new FieldCellType();
                            newCellType.tableName = '' + sheet.parent.getActiveSheetIndex();
                            newCellType.fieldName = newField;
                            newCellType.fieldType = ['input', 'text', '200'];
                            newCellType.detailIndex = bindingPath.replace(`${sheet.name()}.detail_`, '');
                            newCellType.uuid = utils.UUID();
                            newCellType.label = label;
                            sheet.getCell(dataRow, currentColIndex).cellType(newCellType)
                                .textIndent(2.5)
                                .backgroundImageLayout(window.GC.Spread.Sheets.ImageLayout.none)
                                .backgroundImage(newCellType.getFieldTypeImage());
                            currentColIndex--;
                        }
                    }
                    newColumns.push(new window.GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField));
                    const cellType = sheet.getCell(dataRow, col + index).cellType();
                    sheet.getCell(dataRow, currentColIndex).cellType(cellType)
                        .textIndent(2.5)
                        .backgroundImageLayout(window.GC.Spread.Sheets.ImageLayout.none)
                        .backgroundImage(cellType.getFieldTypeImage());
                    currentColIndex--;

                    // 最左边一行 左边加一行单独处理
                    if (index === 0 && ((-1) + col) === pos) {
                        let fieldIndex = 0;
                        for (let colIndex = 0; colIndex < addCols; colIndex++) {
                            let newField = `zd${++fieldIndex}`;
                            while (fieldNames.indexOf(newField) > -1) {
                                newField = `zd${++fieldIndex}`;
                            }
                            fieldNames.push(newField);
                            const label = `${getLabel('502711', '字段') + fieldIndex}`;
                            newColumns.push(new window.GC.Spread.Sheets.Tables.TableColumn(newField).name(label).dataField(newField));
                            const newCellType = new FieldCellType();
                            newCellType.tableName = '' + sheet.parent.getActiveSheetIndex();
                            newCellType.fieldName = newField;
                            newCellType.fieldType = ['input', 'text', '200'];
                            newCellType.detailIndex = bindingPath.replace(`${sheet.name()}.detail_`, '');
                            newCellType.label = label;
                            newCellType.uuid = utils.UUID();
                            sheet.getCell(dataRow, currentColIndex).cellType(newCellType)
                                .textIndent(2.5)
                                .backgroundImageLayout(window.GC.Spread.Sheets.ImageLayout.none)
                                .backgroundImage(newCellType.getFieldTypeImage());
                            currentColIndex--;
                        }
                    }
                }
                newTable.bindColumns(newColumns.reverse());
                newTable.bindingPath(bindingPath);
            }
            sheet.resumePaint();
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
    }
};